package com.starinside.wp.service.impl;

import com.google.common.collect.Lists;
import com.starinside.wp.domain.Children;
import com.starinside.wp.domain.ChildrenHealthBaseInfo;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.query.ChildrenQuery;
import com.starinside.wp.foundation.query.HealthQuery;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.ChildChildrenHealthListVo;
import com.starinside.wp.foundation.vo.ChildrenHealthListVo;
import com.starinside.wp.repository.ChildrenHealthBaseInfoRepository;
import com.starinside.wp.service.ChildrenHealthService;
import com.starinside.wp.service.ChildrenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2017/11/1
 * *******************************
 */
@Service
@Slf4j
public class ChildrenHealthServiceImpl implements ChildrenHealthService {
    @Autowired
    private ChildrenService childrenService;

    @Autowired
    private ChildrenHealthBaseInfoRepository childrenHealthBaseInfoRepository;

    @Override
    public Page<ChildrenHealthBaseInfo> getChildChildrenHealthPage(String childrenId, PagerQuery pagerQuery) {
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());
        Page<ChildrenHealthBaseInfo> baseInfoList = childrenHealthBaseInfoRepository.findAllByChildrenIdOrderByCreatedDesc(childrenId, pageRequest);
        return baseInfoList;
    }



    @Override
    public Map findHealthInfoDetail(String id) {
        Map map = new HashMap();
        Optional<ChildrenHealthBaseInfo> healthBaseInfoOptional = childrenHealthBaseInfoRepository.findById(id);
        if(!healthBaseInfoOptional.isPresent()){
            return null;
        }

        ChildrenHealthBaseInfo healthBaseInfo = healthBaseInfoOptional.get();
        Children children = childrenService.getById(healthBaseInfo.getChildrenId());
        map.put("children", children);
        map.put("healthBaseInfo", healthBaseInfo);
        return map;
    }

}
